/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.dao.cd.impl;

import java.io.File;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import who.tmap.bo.cd.CDTypes;
import who.tmap.common.TMapException;
import who.tmap.common.TMapFactory;
import who.tmap.dao.TMapDAOException;
import who.tmap.dao.cd.CartographicDisplayDAO;

/**
 * @author stl
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CDDAOFactory implements TMapFactory, CDTypes {

    private static CDDAOFactory instance =null;
    
    /**
     * Private constructor because it is a singleton
     *
     */
    private CDDAOFactory() {
    }
    
    /**
     * Lazy instanciation of the factory instance
     * @return
     */
    public static synchronized CDDAOFactory getInstance() {
        if ( instance == null ) {
            instance =new CDDAOFactory();
        }
        
        return instance;
    }
    
    /**
     * Instanciates cartographic display daos
     * @param cartographic display type
     * @return
     * @throws TMapDAOException
     * @throws TMapException
     */
    public CartographicDisplayDAO get( int cdType ) throws TMapDAOException {       
    	CartographicDisplayDAO dao =null;
        
        switch( cdType ) {
        	case POINT_BASIC:
        		dao =new CDPointBasicDAOImpl();
        		break;
        		
        	case POINT_DATA_DRIVEN_SIZE:
        		dao =new CDPointDataDrivenSizeDAOImpl();
        		break;      
        		
        	case POINT_DATA_DRIVEN_SYMBOL:
        		dao =new CDPointDataDrivenSymbolDAOImpl();
        		break;
        		
        	case POINT_DATA_DRIVEN_SYMBOL_AND_SIZE:
        		dao =new CDPointDataDrivenSymbolAndSizeDAOImpl();
        		break;        		
        
        	case PROPORTIONAL_SYMBOLS:
        		dao =new CDProportionalSymbolsDAOImpl();
        		break;
        		
        	case COLOR_PATTERN:
        		dao =new CDColorPatternDAOImpl();
        		break;    
        		
        	case DOT_DENSITY:
        		dao =new CDRandomDotDensityDAOImpl();
        		break;

        	default:
        	    throw new TMapDAOException( "Invalid CD DAO type [" + cdType + "]" );
        }
        
        return dao;
    }
    
    public CartographicDisplayDAO get( File file ) throws TMapDAOException {
    	int cdType =-1;
    	
    	try {
	        SAXBuilder builder = new SAXBuilder();
	        //builder.setValidation(true);
	        builder.setIgnoringElementContentWhitespace(true);
	        Document doc = builder.build( new File(file.getAbsolutePath()) );
	        
	        Element rootElt =doc.getRootElement();
        
    		if ( rootElt.getChild( "ColorPattern" ) != null ) {
    			cdType =CDTypes.COLOR_PATTERN;
    		} else if ( rootElt.getChild( "ProportionalSymbols" ) != null ) {
    			cdType =CDTypes.PROPORTIONAL_SYMBOLS;
    		} else if ( rootElt.getChild( "RandomDensity" ) != null ) {
    			cdType =CDTypes.DOT_DENSITY;
    		} else if ( rootElt.getChild( "PointBasic" ) != null ) {
    			cdType =CDTypes.POINT_BASIC;
    		} else if ( rootElt.getChild( "PointDataDrivenSize" ) != null ) {
    			cdType =CDTypes.POINT_DATA_DRIVEN_SIZE;
    		} else if ( rootElt.getChild( "PointDataDrivenSymbol" ) != null ) {
    			cdType =CDTypes.POINT_DATA_DRIVEN_SYMBOL;
    		} else if ( rootElt.getChild( "PointDataDrivenSymbolAndSize" ) != null ) {
    			cdType =CDTypes.POINT_DATA_DRIVEN_SYMBOL_AND_SIZE;
    		}
	    		
    	} catch( Exception e ) {
    		throw new TMapDAOException( e );
    	}
        
        return get( cdType );
    }
}
